<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <?= $show_code; ?>
        <title><?php echo $this->config->item('title_page'); ?></title>
        <script type="text/javascript" src="<?php echo base_url(); ?>js/TreeListFilter.js"></script>
    </head>
    <script type="text/javascript">
        $(function() {

            showFirstForm();

            $('#liMSAutoType').css("cursor", "pointer");
            $('#txtSearchAutoPart').treeListFilter('#autopart_root', 200);
            show_list_autopart_type(null, null);

            $('#liMSAutoType').click(function() {
                $('#txtTypeAutoPart').val('');
                showFirstForm();
                $('#btnAutoPartAdd').css('display', 'inline-block');
                $('#btnAutoPartEdit').css('display', 'none');
                $('#btnAutoPartDelete').css('display', 'none');
            });

            $('#btnAutoPartAdd').click(function() {
                showSecoundForm();
                if ($('#txtTypeAutoPart').val() === 'ประเภทอะไหล่หลัก') {
                    $('#txtTypeAutoPart').val('ประเภทอะไหล่ย่อย');
                } else if ($('#txtTypeAutoPart').val() === 'ประเภทอะไหล่ย่อย') {
                    $('#txtTypeAutoPart').val('ยี่ห้ออะไหล่');
                } else {
                    $('#txtTypeAutoPart').val('ประเภทอะไหล่หลัก');
                }

                $('#txtTypeAutoPart').val();
                $('#txtParentAutoPart').val($('#txtAutoPartName').val());
                $('#txtAutoPartName').val('');
                $('#txtAutoPartName').attr('disabled', false);
            });

            $('#btnAutoPartEdit').click(function() {
                showSecoundForm();
                $('#btnAutoPartSave').css('display', 'none');
                $('#btnAutoPartUpdate').css('display', 'inline-block');
                $('#txtAutoPartName').attr('disabled', false);
            });


            $('#btnAutoPartSave').click(function() {
                if ($('#txtAutoPartName').val() !== "") {
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>setting/home/save_autoparts",
                        data: {parent_id: $('#hidParentId').val(), type_name: $('#txtAutoPartName').val()},
                        dataType: "json",
                        success: function(data) {
                            $("#ul_autopart_list").empty();
                            show_list_autopart_type(data.list_autopart, data.count_autopart);
                            alert('บันทึกเสร็จเรียบร้อย');
                            showFirstForm();
                        }
                    });
                } else {
                    alert('กรุณากรอกชื่อประเภท');
                }
            });


            $('#btnAutoPartUpdate').click(function() {
                if ($('#txtAutoPartName').val() !== "") {
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>setting/home/update_autoparts",
                        data: {id: $('#hidParentId').val(), type_name: $('#txtAutoPartName').val()},
                        dataType: "json",
                        success: function(data) {
                            $("#ul_autopart_list").empty();
                            show_list_autopart_type(data.list_autopart, data.count_autopart);
                            alert('บันทึกเสร็จเรียบร้อย');
                            showFirstForm();
                        }
                    });
                } else {
                    alert('กรุณากรอกชื่อประเภท');
                }
            });

            $('#btnAutoPartCancel').click(function() {
                showFirstForm();
            });

            $('#btnAutoPartDelete').click(function() {
                if (confirm('ยืนยันการลบรายการ?') === true) {
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>setting/home/delete_autopart",
                        data: {id: $('#hidParentId').val()},
                        dataType: "json",
                        success: function(data) {
                            $("#ul_autopart_list").empty();
                            show_list_autopart_type(data.list_autopart, data.count_autopart);
                            alert('ลบรายการเสร็จเรียบร้อย');
                            showFirstForm();
                        }
                    });
                } else {
                    return false;
                }
            });


            function showFirstForm() {
                $('#txtAutoPartName').attr('disabled', true);
                $('#txtAutoPartName').val('');
                $('#txtParentAutoPart').val('');
                $('#txtTypeAutoPart').val('');
                $('#hidParentId').val('');
                $('#btnAutoPartAdd').css('display', 'none');
                $('#btnAutoPartEdit').css('display', 'none');
                $('#btnAutoPartDelete').css('display', 'none');
                $('#btnAutoPartSave').css('display', 'none');
                $('#btnAutoPartUpdate').css('display', 'none');
                $('#btnAutoPartCancel').css('display', 'none');
            }

            function showSecoundForm() {
                $('#btnAutoPartAdd').css('display', 'none');
                $('#btnAutoPartEdit').css('display', 'none');
                $('#btnAutoPartDelete').css('display', 'none');
                $('#btnAutoPartSave').css('display', 'inline-block');
                $('#btnAutoPartCancel').css('display', 'inline-block');
            }
            function show_list_autopart_type(list, count) {
                var autopart_list = list === null ? '<?php echo $list_autopart; ?>' : list;
                var count_autopart = count === null ? <?php echo $count_autopart; ?> : count;
                $("#ul_autopart_list").append(autopart_list);

                for (var i = 0; i < count_autopart; i++) {
                    $('#liAutoPart' + i).on('click', {value: $('#liAutoPart' + i).attr('value')
                        , type: $('#liAutoPart' + i).attr('auto_type')}, function(event) {
                        $('#btnAutoPartSave').css('display', 'none');
                        $('#btnAutoPartUpdate').css('display', 'none');
                        $('#btnAutoPartCancel').css('display', 'none');
                        if (event.data.type === 'brand') {
                            $('#btnAutoPartAdd').css('display', 'none');
                            $('#btnAutoPartEdit').css('display', 'inline-block');
                            $('#btnAutoPartDelete').css('display', 'inline-block');
                            $('#txtTypeAutoPart').val('ยี่ห้ออะไหล่');
                        } else {
                            $('#btnAutoPartAdd').css('display', 'inline-block');
                            $('#btnAutoPartEdit').css('display', 'inline-block');
                            $('#btnAutoPartDelete').css('display', 'inline-block');
                        }

                        if (event.data.type === 'main_type') {
                            $('#txtTypeAutoPart').val('ประเภทอะไหล่หลัก');
                        } else if (event.data.type === 'sub_type') {
                            $('#txtTypeAutoPart').val('ประเภทอะไหล่ย่อย');
                        }

                        $.ajax({
                            type: "POST",
                            url: "<?php echo base_url(); ?>setting/home/get_autopart_type",
                            data: {id: event.data.value},
                            dataType: "json",
                            success: function(data) {
                                $('#txtAutoPartName').attr('disabled', true);
                                $('#txtAutoPartName').val(data.name);
                                $('#txtParentAutoPart').val(data.parent_name);
                                $('#hidParentId').val(event.data.value);
                            }
                        });
                    });
                }
            }
        }
        );</script>
    <script type="text/javascript">
        $(function() {

            showFirstForm();

            $('#liMSTruckType').css("cursor", "pointer");
            $('#txtSearchTruck').treeListFilter('#truck_root', 200);
            show_list_truck_type(null, null);

            $('#liMSTruckType').click(function() {
                $('#txtTypeTruck').val('');
                showFirstForm();
                $('#btnTruckAdd').css('display', 'inline-block');
                $('#btnTruckEdit').css('display', 'none');
                $('#btnTruckDelete').css('display', 'none');
            });

            $('#btnTruckAdd').click(function() {
                showSecoundForm();
                if ($('#txtTypeTruck').val() === 'ยี่ห้อรถสิบล้อ') {
                    $('#txtTypeTruck').val('รุ่นรถสิบล้อ');
                } else {
                    $('#txtTypeTruck').val('ยี่ห้อรถสิบล้อ');
                }

                $('#txtTypeTruck').val();
                $('#txtParentTruck').val($('#txtTruckName').val());
                $('#txtTruckName').val('');
                $('#txtTruckName').attr('disabled', false);
            });

            $('#btnTruckEdit').click(function() {
                showSecoundForm();
                $('#btnTruckSave').css('display', 'none');
                $('#btnTruckUpdate').css('display', 'inline-block');
                $('#txtTruckName').attr('disabled', false);
            });

            $('#btnTruckSave').click(function() {
                if ($('#txtTruckName').val() !== "") {
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>setting/home/save_truck_type",
                        data: {parent_id: $('#hidTParentId').val(), type_name: $('#txtTruckName').val()},
                        dataType: "json",
                        success: function(data) {
                            $("#ul_truck_list").empty();
                            show_list_truck_type(data.list_truck, data.count_truck);
                            alert('บันทึกเสร็จเรียบร้อย');
                            showFirstForm();
                        }
                    });
                } else {
                    alert('กรุณากรอกชื่อประเภท');
                }
            });

            $('#btnTruckUpdate').click(function() {
                if ($('#txtTruckName').val() !== "") {
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>setting/home/update_truck_type",
                        data: {id: $('#hidTParentId').val(), type_name: $('#txtTruckName').val()},
                        dataType: "json",
                        success: function(data) {
                            $("#ul_truck_list").empty();
                            show_list_truck_type(data.list_truck, data.count_truck);
                            alert('บันทึกเสร็จเรียบร้อย');
                            showFirstForm();
                        }
                    });
                } else {
                    alert('กรุณากรอกชื่อประเภท');
                }
            });

            $('#btnTruckCancel').click(function() {
                showFirstForm();
            });

            $('#btnTruckDelete').click(function() {
                if (confirm('ยืนยันการลบรายการ?') === true) {
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>setting/home/delete_truck_type",
                        data: {id: $('#hidTParentId').val()},
                        dataType: "json",
                        success: function(data) {
                            $("#ul_truck_list").empty();
                            show_list_truck_type(data.list_truck, data.count_truck);
                            alert('ลบรายการเสร็จเรียบร้อย');
                            showFirstForm();
                        }
                    });
                } else {
                    return false;
                }
            });

            function showFirstForm() {
                $('#txtTruckName').attr('disabled', true);
                $('#txtTruckName').val('');
                $('#txtParentTruck').val('');
                $('#txtTypeTruck').val('');
                $('#hidTParentId').val('');
                $('#btnTruckAdd').css('display', 'none');
                $('#btnTruckEdit').css('display', 'none');
                $('#btnTruckDelete').css('display', 'none');
                $('#btnTruckSave').css('display', 'none');
                $('#btnTruckUpdate').css('display', 'none');
                $('#btnTruckCancel').css('display', 'none');
            }

            function showSecoundForm() {
                $('#btnTruckAdd').css('display', 'none');
                $('#btnTruckEdit').css('display', 'none');
                $('#btnTruckDelete').css('display', 'none');
                $('#btnTruckSave').css('display', 'inline-block');
                $('#btnTruckUpdate').css('display', 'none');
                $('#btnTruckCancel').css('display', 'inline-block');
            }

            function show_list_truck_type(list, count) {
                var list_truck = list === null ? '<?php echo $list_truck; ?>' : list;
                var count_truck = count === null ? <?php echo $count_truck; ?> : count;
                $("#ul_truck_list").append(list_truck);

                for (var i = 0; i < count_truck; i++) {
                    $('#liTruck' + i).on('click', {value: $('#liTruck' + i).attr('value')
                        , type: $('#liTruck' + i).attr('truck_type')}, function(event) {
                        $('#btnTruckSave').css('display', 'none');
                        $('#btnTruckUpdate').css('display', 'none');
                        $('#btnTruckCancel').css('display', 'none');
                        if (event.data.type === 'model') {
                            $('#btnTruckAdd').css('display', 'none');
                            $('#btnTruckEdit').css('display', 'inline-block');
                            $('#btnTruckDelete').css('display', 'inline-block');
                            $('#txtTypeTruck').val('รุ่นรถสิบล้อ');
                        } else {
                            $('#btnTruckAdd').css('display', 'inline-block');
                            $('#btnTruckEdit').css('display', 'inline-block');
                            $('#btnTruckDelete').css('display', 'inline-block');
                            $('#txtTypeTruck').val('ยี่ห้อรถสิบล้อ');
                        }


                        $.ajax({
                            type: "POST",
                            url: "<?php echo base_url(); ?>setting/home/get_truck_type",
                            data: {id: event.data.value},
                            dataType: "json",
                            success: function(data) {
                                $('#txtTruckName').attr('disabled', true);
                                $('#txtTruckName').val(data.name);
                                $('#txtParentTruck').val(data.parent_name);
                                $('#hidTParentId').val(event.data.value);
                            }
                        });
                    });
                }
            }
        });
    </script>
    <body>
        <div class="panel panel-default body-contain ">
            <div class="panel-heading"><span class="glyphicon glyphicon-cog"></span>
                <strong>  การตั้งค่าระบบ (System Setting)</strong>
            </div>
            <div class="panel-body">
                <?php $this->load->view('contains/menu_module_bar'); ?>
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="panel panel-default">
                            <!-- Default panel contents -->
                            <div class="panel-heading"><strong>ตั้งค่ารายการประเภทอะไหล่</strong></div>
                            <div class="panel-body">
                                <div class="panel panel-default" style="float:left;width:40%;height:380px;margin-right: 20px;overflow:auto;">
                                    <ul id="autopart_root">
                                        <li style="margin-left: -10px"><h4><span id="liMSAutoType" class="label label-primary"><strong>ประเภทอะไหล่</strong></span></h4>
                                            <ul id="ul_autopart_list">

                                            </ul>
                                        </li>
                                    </ul>
                                </div>
                                <div class="panel panel-default" style="float: left;width:57%;height:380px">
                                    <div class="panel-body">
                                        <form role="form">
                                            <div class="form-group">
                                                <label>ค้นหาประเภทอะไหล่</label>
                                                <input id="txtSearchAutoPart" class="form-control"  />
                                            </div>
                                            <ul class="nav nav-tabs"></ul><p>
                                            <div class="form-group">
                                                <label>ประเภท</label>
                                                <input id="txtTypeAutoPart" class="form-control" disabled/>
                                            </div>
                                            <div class="form-group">
                                                <label>ชื่อประเภทที่สืบทอด</label>
                                                <input id="txtParentAutoPart" class="form-control" disabled/>
                                                <input type="hidden" id="hidParentId" name="hidParentId" />
                                            </div>
                                            <div class="form-group">
                                                <label>ชื่อประเภทอะไหล่</label>
                                                <input id="txtAutoPartName" name="txtAutoPartName" class="form-control" disabled/>
                                            </div>
                                            <div class="text-center">
                                                <button id="btnAutoPartAdd" type="button" class="btn btn-default">เพิ่ม</button>
                                                <button id="btnAutoPartEdit" type="button" class="btn btn-default">แก้ไข</button>
                                                <button id="btnAutoPartDelete" type="button" class="btn btn-default">ลบ</button>
                                                <button id="btnAutoPartSave" type="button" class="btn btn-default" style="display:none">บันทึก</button>
                                                <button id="btnAutoPartUpdate" type="button" class="btn btn-default" style="display:none">บันทึก</button>
                                                <button id="btnAutoPartCancel" type="button" class="btn btn-default" style="display:none">ยกเลิก</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="panel panel-default">
                            <!-- Default panel contents -->
                            <div class="panel-heading"><strong>ตั้งค่ารายการประเภทรถสิบล้อ</strong></div>
                            <div class="panel-body">
                                <div class="panel panel-default" style="float:left;width:40%;height:380px;margin-right: 20px;overflow:auto;">
                                    <ul id="truck_root">
                                        <li style="margin-left: -10px"><h4><span id="liMSTruckType" class="label label-primary"><strong>ประเภทรถบรรทุก</strong></span></h4>
                                            <ul id="ul_truck_list">

                                            </ul>
                                        </li>
                                    </ul>
                                </div>
                                <div class="panel panel-default" style="float: left;width:57%;height:380px">
                                    <div class="panel-body">
                                        <form role="form">
                                            <div class="form-group">
                                                <label>ค้นหาประเภทสิบล้อ</label>
                                                <input id="txtSearchTruck" class="form-control"  />
                                            </div>
                                            <ul class="nav nav-tabs"></ul><p>
                                            <div class="form-group">
                                                <label>ประเภท</label>
                                                <input id="txtTypeTruck" class="form-control" disabled/>
                                            </div>
                                            <div class="form-group">
                                                <label>ชื่อประเภทที่สืบทอด</label>
                                                <input id="txtParentTruck" class="form-control" disabled/>
                                                <input type="hidden" id="hidTParentId" name="hidTParentId" />
                                            </div>
                                            <div class="form-group">
                                                <label>ชื่อประเภทสิบล้อ</label>
                                                <input id="txtTruckName" name="txtTruckName" class="form-control" disabled/>
                                            </div>
                                            <div class="text-center">
                                                <button id="btnTruckAdd" type="button" class="btn btn-default">เพิ่ม</button>
                                                <button id="btnTruckEdit" type="button" class="btn btn-default">แก้ไข</button>
                                                <button id="btnTruckDelete" type="button" class="btn btn-default">ลบ</button>
                                                <button id="btnTruckSave" type="button" class="btn btn-default" style="display:none">บันทึก</button>
                                                <button id="btnTruckUpdate" type="button" class="btn btn-default" style="display:none">บันทึก</button>
                                                <button id="btnTruckCancel" type="button" class="btn btn-default" style="display:none">ยกเลิก</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>
